home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Sample Code / Games / MoofWars / MoofWars Encoder 8⁄15⁄96 / •Sources / main.cp < prev    next >
Encoding:
Text File  |  1996-08-19  |  8.5 KB  |  385 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #    Main.cp
  4. #    
  5. #    An extremely basic encoder utility to assist in building all of the various
  6. #   "compiled" resources used by MoofWars.  Many of the encoder utilities are actually
  7. #   handled by things in the standard TGraphic class, so the main reason for pre-encoding
  8. #   the graphics is to improve the time to load the graphics in the game.
  9. #   
  10. #
  11. #    Author: Timothy Carroll
  12. #    Apple Developer Technical Support
  13. #    timc@apple.com
  14. #
  15. #    Modification History: 
  16. #
  17. #    8/15/96        TMC     Initial Release
  18. #
  19. #    Copyright © 1996 Apple Computer, Inc., All Rights Reserved
  20. #
  21. #
  22. #    You may incorporate this sample code into your applications without
  23. #    restriction, though the sample code has been provided "AS IS" and the
  24. #    responsibility for its operation is 100% yours.  However, what you are
  25. #    not permitted to do is to redistribute the source as "DSC Sample Code"
  26. #    after having made changes. If you're going to re-distribute the source,
  27. #    we require that you make it clear in the source that the code was
  28. #    descended from Apple Sample Code, but that you've made changes.
  29. #
  30. *************************************************************************************/
  31.  
  32. #include "GridEncode.h"
  33. #include "ICL8Encode.h"
  34. #include "PictEncode.h"
  35. #include "PICTMask.h"
  36.  
  37. /*********************************************************************************
  38.     Globals    
  39. *********************************************************************************/
  40.  
  41. CTabHandle            gAppColorTable = NULL;
  42. static Boolean         gDone = false;
  43.  
  44.  
  45. /*********************************************************************************
  46.     Functions    
  47. *********************************************************************************/
  48.  
  49. void main (void);
  50.  
  51. OSStatus InitApp (void);
  52. OSStatus RunApp (void);
  53. OSStatus QuitApp (void);
  54. OSStatus HandleMenuCommand (SInt16 theMenu, SInt16 theItem);
  55. OSStatus GetFiles (SInt16 *input, SInt16 *output);
  56.  
  57.  
  58.  
  59.  
  60.  
  61. void main (void)
  62. {
  63.     OSStatus theErr = noErr;
  64.     int loop;
  65.     
  66.     // Standard Mac Init
  67.     InitGraf(&qd.thePort);
  68.     InitFonts();
  69.     InitWindows();
  70.     InitMenus();
  71.     TEInit();
  72.     InitDialogs(nil);
  73.     InitCursor();
  74.  
  75.     MaxApplZone();
  76.     
  77.     for (loop = 0; loop < kNumberOfMasters; loop++)
  78.         MoreMasters();
  79.  
  80.     // Initialize Application Specifics
  81.     theErr = InitApp();
  82.     FAIL_OSERR (theErr, "\pFailed to initialize the application")
  83.  
  84.     // Run the eventloop
  85.     theErr = RunApp();
  86.     
  87.     error:
  88.     // On an error, the application will just quit.  Probably would be nice in a real
  89.     // app to put up a dialog box or something.
  90.     
  91.     theErr = QuitApp();
  92. }
  93.  
  94. OSStatus InitApp (void)
  95. {
  96.     OSStatus    theErr         = noErr;
  97.     Handle        menuBarH     = NULL;
  98.     MenuRef        appleMenuH     = NULL;
  99.     MenuRef        engineMenuH    = NULL;
  100.     
  101.     // read and install the menu bar, adding DA names.
  102.     menuBarH = GetNewMBar(rMenuBar);
  103.     FAIL_NIL (menuBarH, "\pFailed to load the MenuBar")
  104.  
  105.     SetMenuBar(menuBarH);
  106.     DisposeHandle(menuBarH);
  107.     
  108.     appleMenuH = GetMHandle(mAppleMenu);
  109.     FAIL_NIL (appleMenuH, "\pFailed to find the Apple Menu")
  110.     AddResMenu(appleMenuH, 'DRVR');
  111.     
  112.     DrawMenuBar();
  113.      
  114.      goto cleanup;
  115.      
  116.     error:
  117.     
  118.     if (theErr == noErr)
  119.         theErr = paramErr;
  120.     
  121.     cleanup:
  122.     return theErr;
  123. }
  124.  
  125.  
  126. OSStatus RunApp (void)
  127. {
  128.  
  129.     WindowRef    whichWindow = NULL;
  130.     OSStatus    theErr = noErr;
  131.     
  132.     EventRecord    theEvent;
  133.     short         partCode;
  134.     long        menuResult;
  135.     short        theMenu, theItem;
  136.  
  137.     while (!gDone)
  138.     {
  139.         (void) WaitNextEvent(everyEvent,&theEvent,kSleepTime,NULL);
  140.  
  141.         switch (theEvent.what)
  142.         {
  143.             case    nullEvent:
  144.                 break;            
  145.             case    mouseDown:
  146.                 partCode = FindWindow(theEvent.where,&whichWindow);
  147.             
  148.                 switch(partCode)
  149.                 {
  150.                     case    inMenuBar:
  151.                         menuResult = MenuSelect(theEvent.where);
  152.                         theMenu = (menuResult >> 16) & 0x0000FFFF;
  153.                         theItem = (menuResult) & 0x0000FFFF;
  154.                         
  155.                         HandleMenuCommand (theMenu, theItem);
  156.                         HiliteMenu(0);
  157.  
  158.                         break;
  159.  
  160.                     default:
  161.                         break;
  162.                     }
  163.                 break;
  164.         
  165.             case    keyDown:
  166.                 if (theEvent.modifiers & cmdKey)
  167.                     {
  168.                         menuResult = MenuKey((short) theEvent.message & charCodeMask);
  169.                         theMenu = (menuResult >> 16) & 0x0000FFFF;
  170.                         theItem = (menuResult) & 0x0000FFFF;
  171.                     
  172.                         HandleMenuCommand (theMenu, theItem);
  173.                         
  174.                         HiliteMenu(0);
  175.                     }
  176.                 break;
  177.             default:
  178.                 break;
  179.         }
  180.  
  181.     }
  182.  
  183.     return noErr;
  184. }
  185.  
  186.  
  187.  
  188. OSStatus QuitApp (void)
  189. {
  190.     return noErr;
  191. }
  192.  
  193. OSStatus HandleMenuCommand (SInt16 theMenu, SInt16 theItem)
  194. {
  195.     OSStatus theErr = noErr;
  196.  
  197.     switch (theMenu)
  198.     {
  199.         case mAppleMenu:
  200.             if (theItem == iAboutApp)
  201.                 (void) Alert (128, NULL); 
  202.             else
  203.                 {
  204.                 MenuRef        appleMenuH     = NULL;
  205.                 Str255        deskAccName;
  206.                 
  207.                 appleMenuH = GetMHandle(mAppleMenu);
  208.                 FAIL_NIL (appleMenuH, "\pFailed to find the Apple Menu")
  209.                 
  210.                 GetMenuItemText(appleMenuH,theItem,deskAccName);
  211.                 (void) OpenDeskAcc(deskAccName);
  212.                 }
  213.                 
  214.         break;
  215.         
  216.         case mFileMenu:
  217.             switch (theItem)
  218.             {
  219.                 case iEncodeIcons:
  220.                 {
  221.                     SInt16 in, out;
  222.                     theErr = GetFiles (&in, &out);
  223.                     FAIL_OSERR (theErr, "\p Failed to open the files")
  224.                     
  225.                     ICL8Encode (in, out);
  226.     
  227.                     CloseResFile(in);
  228.                     CloseResFile (out);
  229.                 }
  230.                 break;
  231.                 
  232.                 case iEncodePICTs:
  233.                 {
  234.                     SInt16 in, out;
  235.                     theErr = GetFiles (&in, &out);
  236.                     FAIL_OSERR (theErr, "\p Failed to open the files")
  237.                     
  238.                     PICTEncode (in, out);
  239.     
  240.                     CloseResFile(in);
  241.                     CloseResFile (out);
  242.                 }
  243.                 break;
  244.                 
  245.                 case iTriplePICTs:
  246.                 {
  247.                     SInt16 in, out;
  248.                     theErr = GetFiles (&in, &out);
  249.                     FAIL_OSERR (theErr, "\p Failed to open the files")
  250.                     
  251.                     GeneratePICTMasks (in, out);
  252.     
  253.                     CloseResFile(in);
  254.                     CloseResFile (out);
  255.                 }
  256.                 break;
  257.                 
  258.                 case iEncodeTiles:
  259.                 {
  260.                     SInt16 in, out;
  261.                     theErr = GetFiles (&in, &out);
  262.                     FAIL_OSERR (theErr, "\p Failed to open the files")
  263.                     
  264.                     GridTileEncode (in, out);
  265.     
  266.                     CloseResFile(in);
  267.                     CloseResFile (out);
  268.                 }
  269.                 break;
  270.                 
  271.                 case iQuit:
  272.                     gDone = true;
  273.                 break;
  274.             }
  275.     }
  276.     return noErr;
  277.     
  278.     error:
  279.     if (theErr == noErr)
  280.         theErr = paramErr;
  281.     return theErr;
  282. }
  283.  
  284.  
  285. OSStatus GetFiles (SInt16 *input, SInt16 *output)
  286. {
  287.     // This routine displays the standard get and put file dialogs so that the user
  288.     // can select a file to read from, and to create an output file.
  289.     
  290.     StandardFileReply        inputFile;
  291.     StandardFileReply        outputFile;
  292.     SFTypeList                typeList;
  293.     Point                     p;
  294.     short                    inResNum, outResNum;
  295.  
  296.     typeList[0] = 'RSRC';
  297.     typeList[1] = 'rsrc';
  298.     
  299.     p.h = 100; p.v = 100;
  300.     StandardGetFile (NULL, 2, typeList, &inputFile);
  301.     
  302.     if (!inputFile.sfGood)
  303.         return paramErr;
  304.         
  305.     StandardPutFile ("\pSave file as...", "\pUntitled",&outputFile);
  306.     
  307.     if (!(outputFile.sfGood && !outputFile.sfReplacing))
  308.         return paramErr;
  309.     
  310.     inResNum = FSpOpenResFile (&inputFile.sfFile, fsRdPerm);
  311.     FSpCreateResFile(&outputFile.sfFile, 'Doug', 'RSRC', outputFile.sfScript);
  312.     outResNum = FSpOpenResFile (&outputFile.sfFile, fsRdWrPerm);
  313.     
  314.     *input = inResNum;
  315.     *output = outResNum;
  316.     return noErr;
  317. }
  318.  
  319. // This copies a particular resource from the source res file to the destination.
  320. OSStatus CopyResource (SInt16 inRes, SInt16 outRes, OSType theType, SInt16 theID)
  321. {
  322.     OSStatus    theErr = noErr;
  323.     
  324.     Handle        temp = NULL;
  325.     
  326.     // temp can be either a resource or a handle, we use this to tell what the current status is, so
  327.     // that if we need to cleanup, we can do this correctly.
  328.     Boolean        tempIsResource = true; 
  329.     
  330.     // we pass these to GetResInfo so that we can get the actual resource ID.
  331.     SInt16        resID;
  332.     ResType        resType;
  333.     Str255        resName;
  334.  
  335.     SInt16        saveResNum; // Used to save the resource chain info
  336.     
  337.     saveResNum = CurResFile();
  338.  
  339.     UseResFile (inRes);
  340.  
  341.     // attempt to grab the color table resource.  We grab it as a resource because we're going
  342.     // to write it right back out into the output file.
  343.  
  344.     temp = Get1Resource (theType, theID);
  345.     theErr = ResError();
  346.     FAIL_NIL (temp, "\pFailed to load the resource")
  347.     FAIL_OSERR (theErr, "\pFailed to load the resource")
  348.     
  349.     GetResInfo( temp, &resID, &resType, resName ); // we do this just to get the name
  350.     theErr = ResError();
  351.     FAIL_OSERR( theErr, "\pFailed to get info on the resource")
  352.     
  353.     DetachResource (temp);
  354.     theErr = ResError();
  355.     FAIL_OSERR( theErr, "\pFailed to detach the resource from the input chain")
  356.     
  357.     tempIsResource = false;
  358.         
  359.     UseResFile (outRes);
  360.     
  361.     AddResource( temp, theType, theID, resName );
  362.     theErr = ResError();
  363.     FAIL_OSERR (theErr, "\pFailed to add the resource to the file")
  364.     
  365.     tempIsResource = true;
  366.     
  367.     WriteResource( temp );
  368.     theErr = ResError();
  369.     FAIL_OSERR (theErr, "\pFailed to write the resource to the file")
  370.  
  371.     goto cleanup;
  372.     
  373. error:
  374.     if (theErr == noErr)
  375.         theErr = paramErr;
  376. cleanup:
  377.     if (temp != NULL)
  378.         if (tempIsResource)
  379.             ReleaseResource (temp);
  380.         else
  381.             DisposeHandle (temp);
  382.     
  383.     UseResFile (saveResNum);
  384.     return theErr;
  385. }